home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Information / Programming / Mac Programming FAQ / Mac Programming FAQ 1⁄3 next >
Encoding:
Text File  |  1994-12-08  |  29.2 KB  |  644 lines  |  [TEXT/R*ch]

  1. The Public Domain Mac Programming FAQ Answer sheet 2.0
  2. Last update: 29 Nov 94  -Giving Thanks to Steve Jobs-
  3.  
  4. Changes since 31 Oct 94:
  5. * Standalone code section more complete
  6. * Slight content reformatting
  7. * Minor content updates
  8.  
  9. IMPORTANT NOTE: No FAQ can substitute for real documentation (some of 
  10. which is mentioned in this FAQ) If you ask a question in 
  11. comp.sys.mac.programmer which has a good answer in one of the three 
  12. important sources, you will probably not get an answer.  (Think 
  13. Reference, New Inside Macintosh, Macintosh Technical Notes)
  14.  
  15. This sheet was started by and largely exists due to the efforts of Jon 
  16. Watte, whom you may reach as <h+@nada.kth.se>.  Chris Thomas, whom you 
  17. may reach as <thunderone@delphi.com>, is the current caretaker and 
  18. overall responsible person.  If there is anything you find errant, 
  19. missing or in need of an update, please send Chris your submission and 
  20. he will include it.  All FAQ Answer submissions sent will be considered 
  21. to be in the public domain unless stated otherwise (in which case they 
  22. will not be included in this FAQ sheet).
  23.  
  24. LESS IMPORTANT NOTE: This FAQ has recently been reorganized to make it 
  25. easier to find and put Qs.  Suggestions and constructive comments are 
  26. welcome.
  27.  
  28. New submissions should be in the form:
  29. "Q: What question should I answer?
  30.  
  31. A: This one, obviously..."
  32. if you want to definitely see the submission in the next edition.  
  33. Otherwise integration may take a bit longer.
  34.  
  35. This sheet is currently archived on nada.kth.se where you can reach it 
  36. using afs as /afs/nada.kth.se/public/ftp/pub/hacks/mac-f aq/CSMP_PD_FAQ 
  37. or using anonymous FTP (GIVE YOUR E-MAIL ADDRESS AS PASSWORD!) as 
  38. pub/hacks/mac-faq/CSMP_PD_FAQ You can also find it on rtfm.mit.edu unedr 
  39. the name macintosh/programming-faq.  But that's not all! It should be 
  40. available in the Delphi ICONtact Developer SIG [go com mac dev da info 
  41. narrow faq dir], and most likely AOL's Mac Development area [Keyword: 
  42. MDV] as well.
  43.  
  44. This sheet is now posted once a week to the appropriate news groups.  I 
  45. have also started adding mail addresses for vendors mentioned in the 
  46. FAQ; mail me if you're mentioned and feel neglected!
  47.  
  48. This sheet is divided into several parts; each of which covers a general 
  49. area which often gets asked about in the Internet newsgroup 
  50. comp.sys.mac.programmer.  Please download a copy of this answer sheet 
  51. and search it before you post to the 'net, to help reduce bandwidth.
  52.  
  53. There is NO or VERY LIMITED error checking in these code examples, FOR 
  54. BREVITY ONLY.  You should make sure you ALWAYS check ALL return codes, 
  55. and handle any that you are not prepared to deal with appropriately.
  56.  
  57. Current topics:
  58. (search for *number* to find a topic quickly)
  59. (topics changed since last FAQ are marked with "++")
  60.  
  61.  1. Development Tools
  62.     what you need to get started, tool-specific issues
  63.  2. Memory
  64.     handles, large arrays, resource handles
  65.  3. User Interfacing
  66.     menus, windows, events, multitasking
  67.  4. Files
  68.     Mac fopen, wdrefnums, getting the full pathname of files
  69.  5. Imaging
  70.     QuickDraw and how to avoid it
  71.  6. Text
  72.     Text editing packages (#include <limits.h>)
  73.  7. Communications and Networking
  74.     Serial ports, TCP/IP, sockets
  75.  8. Interapplication Communication
  76.     AppleEvents and OSA
  77.  9. Dynamic Linking
  78.     external code resources
  79. 10. Compatibility
  80.     gestalt, glue
  81. 11. Optional System Software
  82.     11.1. QuickTime
  83.       codec details and the lack thereof
  84. 12. Third-Party Solutions
  85.     databases
  86. 13. Dessert
  87.     yummies the Macintosh Way
  88. 14. Contributors
  89.     whodunnit?
  90.  
  91. *1* Development and debugging tools for the Macintosh 
  92.  
  93. 1.1) Q: What do I need to start writing Macintosh software? 
  94.  
  95. A1:
  96. 1. A development environment.
  97. 2. A project.
  98. 3. Smart Friends (tm).
  99.  
  100. What I mean by numbers 2 and 3 is:
  101. 2. Something that _you_ have a personal interest in getting running.
  102. Something to keep you going through those periods of frustration and
  103. fatigue.  Just working through the exercises in a book won't cut it.
  104.  
  105. 3. People who are at least 2 weeks farther along than you, that can help
  106. you avoid the petty (and not-so-petty) wastes of time that await the
  107. neophyte.  These people should preferably be easy to contact (i.e. a local
  108. phone call) and have the same approximate sleep patterns.
  109.  
  110. A2: A Mac, a lot of time, and a few hundred $.  Although you can develop 
  111. software on a Classic-type machine, it is not to be attempted by the 
  112. weak of heart or stressed of time.  If you're doing paid work and/or 
  113. work for a company, a Quadra-class machine or Centris 650 or better is 
  114. almost a must; remember that your time costs your employer much more 
  115. than just your salary.  8 MB is a minimum to run at all comfortably, and 
  116. Virtual Memory is not suited for development work.  Similarly; if you 
  117. don't have at least 80 MB free on your hard disk you need to buy more 
  118. space.
  119.  
  120. You need a development system such as Think C, CodeWarrior C++, MPW C or 
  121. Prograph, you need at least some of the New Inside Mac books (Toolbox 
  122. Essentials, Files, Memory come to mind) and a good entry-level 
  123. third-party book may help.
  124.  
  125. Once you are up to speed on the general layout of the Mac and its 
  126. toolboxes, you should call APDA and order the monthly developer mailing, 
  127. which will give you a CD chock full of documentation, utilities and 
  128. system software once a month.  You will also, obviously, need a CD 
  129. player; one of which Apple's own CD300 is a very good buy at the time of 
  130. writing this.  If you don't have the dough for the monthly mailing 
  131. ($250/year) you can order a _develop_ subscription; this quarterly 
  132. magazine ($30-$50/year) comes with a CD containing most Inside Mac 
  133. documentation.  <apda@appplelink.apple.com>
  134.  
  135. If you don't know how to program, go learn your language of choice 
  136. BEFORE attempting a "real" Mac application.  Programming is a discipline 
  137. often requiring different thought processes than your normal day job.  A 
  138. beginning book, like Lippman: The C++ Primer, or Sydow: Think THINK C 
  139. might help (the latter uses the Think C compiler, while the former can 
  140. be used with any C++ compiler)
  141.  
  142. Another tool which many find a must-have is the Think Reference version 
  143. 2.0.1, containing reference material on the most used parts of the Mac 
  144. toolbox with lightning-fast look-up and mostly correct usage hints and 
  145. code snippets.  It does not cover the newest system additions, nor the 
  146. modern "universal" headers format, though.
  147.  
  148. 1.2) Q: What is the most used Macintosh development system? 
  149.  
  150. A: Out of products on the market, I have no idea; both MPW and Think 
  151. products appear to be used.  Lately, CodeWarrior has come out of nowhere 
  152. and grabbed a large share of the market visibility because they have the 
  153. fastest compiler and they generate PowerPC code as well as 68K code.  
  154. Among hobbyists, the Think products from Symantec are most popular 
  155. because of the low price, and steep educational discounts, and, of 
  156. course, the easily approachable interface! However, now that CodeWarrior 
  157. is available at $59-$99 educational price; anything can happen.
  158.  
  159. The Think C linker only strips dead code on a FILE level basis (and this 
  160. is when you turn on "Smart Linking") The MPW linker (of no specified IQ, 
  161. as someone so eloquently put it :-) strips dead code by the function, as 
  162. does CodeWarrior.  That may be part of the reason the Think C linker is 
  163. ten times faster than the MPW linker - but then how do you explain the 
  164. CodeWarrior linker, which links like the MPW linker, but at speeds 
  165. approaching Think C?
  166.  
  167. In the beginning, the Mac was programmed using Mc68000 assembly or 
  168. Pascal; this was reflected in the Old Inside Mac volumes which only gave 
  169. Pascal-style and assembly-style interfaces to the Mac toolbox.  These 
  170. days, Apple tells us to use C or even better C++ for developing new 
  171. applications, as that will speed up the transition to PowerPC and also 
  172. coming cross-platform efforts.  (Language Systems have a Pascal for 
  173. PowerPC product now, but I don't know about pricing or quality)
  174.  
  175. There are also at least two Fortran compilers, at least three SmallTalk 
  176. implementations (ObjectWorks, SmallTalk/V and SmallTalkAgents) and 
  177. others.  There are ways of stripping SmallTalk apps so they're smaller 
  178. and faster as standalone apps than in the environment.
  179.  
  180. There's also a world-class LISP/CLOS implementation from Apple called 
  181. Macintosh Common Lisp.  Recently, Apple announced that DigiTool has 
  182. licensed MCL with the intent (among other things) to provide a PowerMac 
  183. version and other updates.
  184.  
  185. Metrowerks have a Pascal and a C and C++ compiler for both 68k and Power 
  186. Macs; the package with the three of them is called "CodeWarrior."
  187.  
  188. CSI has MacForth, of which I only know the name and someone who says 
  189. it's pretty good.
  190.  
  191. There is another good Common Lisp implementation: Procyon Common Lisp.  
  192. I don't know if it is actively supported, but Procyon CL is also 
  193. available for DOS, OS/2 and Windows (as Allegro CL/PC) and actively 
  194. developed.
  195.  
  196. 1.3) Q: Where do I find a public domain C compiler for the Mac. 
  197. Is there a GCC for the mac? What about the FSF boycott of Apple 
  198. products? 
  199.  
  200. A: There is no really good solution for a "for-free" C development 
  201. system for the Mac.  GCC has been ported, but requires the MPW shell and 
  202. MPW assembler to run; these have to be bought from APDA.  There is a 
  203. standalone port of GCC 1.37 on nic.switch.ch:software/mac/src/think_c.  
  204. There was a freely available C compiler called Harvest C, which was 
  205. somewhat unstable but usable for smaller programs; it was abandoned by 
  206. the original author Eric W Sink because of a lack of time and later 
  207. taken up by the TopSoft project.
  208.  
  209. The FSF boycott of Apple products means they will not talk to you if you 
  210. ask them for help in doing a Mac port, and they will not incorporate 
  211. your changes into their main code base.  However, they still allow 
  212. others to port GNU stuff to the Mac, and it has been done with most of 
  213. the application-like GNU programs (bison, flex, perl (not really part of 
  214. GNU), ...)
  215.  
  216. gcc-1.37r14 V1.1 standalone is available for ftp at nic.switch.ch: 
  217. software/mac/src/think_c.
  218.  
  219. A fairly stable port of GCC 2.3.3 to MPW is available for ftp at 
  220. atg.apple.com [anyone know the directory?].  A much more solid port of 
  221. GCC 1.37 is available for MPW as well.
  222.  
  223. For those whose main interest is in developing only text based C/C++ 
  224. programs, using GCC under MacMiNT might be appropriate.  MacMiNT is a 
  225. UNIX like operating system ported from the Atari ST which supports many 
  226. freely available UNIX utilities like GCC, GDB, make, tcsh, byacc, perl, 
  227. and more.  MacMiNT stuff can be found at on nic.switch.ch in 
  228. 'software/mac/src/macmint'.
  229.  
  230. 1.4) Q: Are there any other free Mac development platforms? 
  231.  
  232. A: Yerk and Mops.  These are object oriented languages based on the old 
  233. product Neon which itself was based on Forth.  They are available with 
  234. Manuals at oddjob.uchicago.edu (anonymous FTP).
  235.  
  236. Plus, there's MacGambit, MacScheme, xlisp, and MacMETH which is the 
  237. actual ETH (read: Nick Wirth's group) Modula 2, all for free via FTP.  
  238. Also available is MacOberon and MacOberon system/3 betas.
  239.  
  240. And OpenProlog.  And SIOD (Scheme in One Defun) And [mail in more if you 
  241. dare [and please do include ftp addresses if ya can]]
  242.  
  243. 1.5) Q: What's the difference between the MPW, Think and CodeWarrior 
  244. environments?
  245.  
  246. A: The main difference is that Think and CodeWarrior are integrated 
  247. environments, while MPW provides you with a command-line shell for your 
  248. Mac and tools to use in it.  MPW also has a slightly higher systems 
  249. demand and a much slower linker.
  250.  
  251. The good thing about MPW is that you can write scripts and make files to 
  252. do anything you want in the way you want it.  Think or CW still don't 
  253. have a viable solution to do a build that requires more than one link 
  254. operation, or has more than one destination file.
  255.  
  256. For the MPW environment, there are three source level debuggers; SADE, 
  257. SourceBug and Voodoo Monkey.  The latter is an experimental debugger 
  258. with support for threads debugging; the middle is bundled with MPW while 
  259. SADE has to be bought separately (but is fully scriptable in its own 
  260. scripting language)
  261.  
  262. The Think environments have their own integrated debuggers; the Think 
  263. Pascal one has a lot of useful features while the Think C/C++ one is a 
  264. little more basic (but is gaining in functionality with each release) 
  265. Stepping through source code and looking at variables is generally 
  266. faster and easier in Think than the MPW debuggers.
  267.  
  268. Metrowerks has their own debugger which works like the MPW debuggers; i e 
  269. it runs the application standalone and pokes at it from the outside, 
  270. while the Think debuggers run the application "wrapped" in a special 
  271. environment, making for some subtle interferences with your heap (which 
  272. you usually don't notice).  The Metrowerks Debugger is Thread 
  273. Manager-saavy.
  274.  
  275. CodeWarrior is by far the fastest compiler; it can compile to 68k code 
  276. on a Power Macintosh for really fast compiles.  Symantec C++ (a k a 
  277. "Think C++") is the most complete C++.  MPW has the most powerful 
  278. environment.
  279.  
  280. Any commercial Mac developer should have all three of CodeWarrior, Think 
  281. and MPW of whatever language they prefer (Pascal, C or C++), though some 
  282. people seem to be able to get by with just CodeWarrior and MPW.  An 
  283. ever-increasing number of people get by with just CodeWarrior.
  284.  
  285. 1.6) Q: What is a good low-level debugger for the Mac? 
  286.  
  287. A: MacsBug is freely available for ftp from ftp.apple.com; log in as 
  288. user anonymous and give your FULL e-mail address as password.  MacsBug 
  289. is your basic monitor-type debugger that takes a few hundred Ks of 
  290. memory, and lets you break, step, disassemble, look at the stack etc of 
  291. most anything running on your Mac.  Since it's free (it's also on the 
  292. developer CDs) and provides most of the functionality you need, this is 
  293. a popular choice.
  294.  
  295. TMON is another debugger which sports a more mac-like interface; it 
  296. provides windows and uses the mouse.  It can take as little or much 
  297. memory as you want by excluding or including certain areas of 
  298. functionality.  A nice touch is the 6502 disassembler that you can use 
  299. to de-bug the code the IOP processors run on the Mac IIfx and Quadra 
  300. 900/950.
  301.  
  302. Jasik Designs have a debugger called The Debugger which can do both low- 
  303. and high-level debugging, with or without source and for all types of 
  304. code, application, code resources, everything.  This is the debugger of 
  305. choice for many large developers because of its high power and many 
  306. features not found anywhere else.  However; newcomers beware! This is 
  307. the Lamborghini of debuggers; if you know how to drive it, it is the 
  308. fastest way from A to B; if you don't, you'll just end up in the ditch.  
  309. As a lady put it: "The man wouldn't know a user interface if it bit him 
  310. in the rear."
  311.  
  312. 1.7) Q: Are there any visual developments environments for the 
  313. Mac (comparable to Visual C++)?
  314.  
  315. A: There is no Visual C++ as such.  However, there is a C++ 
  316. parser/editor called ObjectMaster which provides good browsing and 
  317. editing capabilities if you already have a C++ compiler.  Think C++ also 
  318. comes with a browser built-in, and you can draw dialogs/windows using 
  319. plain old ResEdit, even for your custom view types.
  320.  
  321. Symantec C++ 7.0 also bundles a view editor/code generator called Visual 
  322. Architect; it is fairly complete and has a good level of integration 
  323. into the Think Project Manager.
  324.  
  325. AppMaker is a GUI builder/code generator.  Granted, it's not as nice as 
  326. VC++, but it's quite a product in any case.
  327.  
  328. MarksMan version 3.0 has totally revised TCL templates, and now 
  329. generates well-thought-out TCL code.  It can also generate ANSI C code 
  330. etc.
  331.  
  332. Also, Neuron Data has their UI tool called Open Interface, which is 
  333. better than VC++ and creates code portable across 35 platforms.  
  334. Unfortunately it's $2500 per developer per platform.  There's also two 
  335. other cross-platform products called XVT and Galaxy, the former has 
  336. gotten flak on UseNet while the latter reportedly is the premier 
  337. cross-platform application builder framework; with everything from 
  338. styled text to network support.
  339.  
  340. There is a fully visual, dynamic, object oriented data-flow-driven 
  341. programming language for the Mac called Prograph Corporate Programming 
  342. Something-or-other (CPX).  It is expensive ($1500) but offers a built-in 
  343. database, easy interfaces to existing data bases, very high productivity 
  344. in implement-test-debug cycling and also offers cross-platform 
  345. capabilities (it comes with a class library which, when your code is 
  346. written using it, works under Windows after re-compilation)
  347.  
  348. SmalltalkAgents comes with a GUI builder, which lets you draw your
  349. interface, and then outputs the code for you.
  350.  
  351. If you'd rather do Common Lisp, Macintosh Common Lisp offers a Common 
  352. Lisp Object System with support for most Mac interface items; you can 
  353. edit code while it is running and build stand-alone applications.
  354.  
  355. However, all of these tools generate rather larger binaries with larger 
  356. system demands than a program written in C.  On the other hand; C++ 
  357. programs require more memory and disk space than programs written in 
  358. assembly.  It's a trade-off, and I believe this type of tools is the 
  359. wave of the near future.
  360.  
  361. 1.8) Q: What class libraries are there for the Mac? 
  362.  
  363. A: Apart from the libraries mentioned above, there are three contenders: 
  364. MacApp, TCL, and PowerPlant.  "Bedrock" will never be released as a 
  365. product, although parts of it surface in TCL 2.0 and other parts will be 
  366. the base for the OpenDoc Parts Framework.
  367.  
  368. MacApp is a heavy-duty class library that has tons of features and a 
  369. steep learning curve; it runs under MPW with Pascal or C++, and also 
  370. under Think Pascal 4.0 A major application written in MacApp is 
  371. PhotoShop.
  372.  
  373. TCL stands for Think Class Library and comes with Think Pascal, C or 
  374. C++.  It is a smaller library that still fills most peoples needs; since 
  375. Think C implements a subset of C++ (the most important OO concepts such 
  376. as virtual functions and inheritance) and the TCL is carefully written 
  377. not to take advantage of any C++ features not in Think C, you can use it 
  378. with Think C.  A major application written in TCL is Lotus 1-2-3.  (TCL 
  379. 1.1.3) Starting with Symantec C++ 7.0, Think Class Library 2.0 using 
  380. templates and "real" C++ objects is shipping.
  381.  
  382. PowerPlant is the Metrowerks CodeWarrior offering; it's written by the 
  383. guy who designed the Think Class Library, but it has a lot of 
  384. differences from the original TCL; for one, it's not a monolithic one 
  385. base class framework.  On the other hand, it has some catching up to do 
  386. before it reaches the level of MacApp.  It is gaining quite fast on TCL, 
  387. but isn't all there yet.
  388.  
  389.  
  390. 1.9) Q: How should I debug and test my software? 
  391.  
  392. A: Get ahold of, and install, the extensions DoubleTrouble, 
  393. DisposeResource and EvenBetterBusError.  They will catch 80% of any 
  394. memory related bugs you may have, including many bugs that follow NULL 
  395. handles or pointers.
  396.  
  397. A low-level debugger is required, and while you install it, install the 
  398. "leaks" dcmd which will help you catch memory leaks in your application.  
  399. All of these tools are available from ftp.apple.com.
  400.  
  401. 1.10) Q: Are there any good Mac programming magazines?
  402.  
  403. A: One Mac programming magazine I know of is MacTech Magazine (formerly 
  404. MacTutor).  It covers a variety of Mac programming topics on various 
  405. levels.  Operating independently from Apple, it has a lot of stuff for 
  406. the beginning Mac programmer, as well as occasional nuggets for the more 
  407. experienced of us.  <custservice@xplain.com>
  408.  
  409. Another VERY GOOD Magazine is _develop_ which is put out by Apple four 
  410. times a year; it comes with a CD containing code for all articles ever 
  411. published in _develop_, and a lot of documentation and system software 
  412. freebies as well.  $30/year in the US.  <dev.subs@applelink.apple.com>
  413.  
  414. 1.11) Q: What about protected memory? I'm sick and tired of re-booting 
  415. when my application crashes.
  416.  
  417. A: Write better software! 
  418.  
  419. Or install The Debugger from Jasik Designs, which can provide your 
  420. application with write-protection of critical parts of memory.  This may 
  421. only work for 030 Macs, though.
  422.  
  423. Making the Mac OS memory-protected is tricky, because applications expect 
  424. to be able to write to low memory, the system heap, temporary memory, 
  425. window lists, and even each other's heaps in some interapplication 
  426. communication solutions that date back to before AppleEvents and the PPC 
  427. Toolbox.
  428.  
  429. 1.12) Q: I have this library written in (Think) Pascal that I want to use 
  430. from Think C/Symantec C, but I get link errors/don't know how to do it.  
  431. What should I do?
  432.  
  433. A: Start by writing a .h file describing the interface.  Remember to 
  434. declare the Pascal functions "pascal".  Build a library with Think Pascal 
  435. and convert it with oConv.
  436.  
  437. Do you get link errors on symbols defined in your Pascal lib? Check the 
  438. capitalisation used.
  439.  
  440. Do you get errors on symbols like LMUL and LDIV? Those functions are 
  441. defined in the Think Pascal library Runtime.lib or uRuntime.lib.  Include 
  442. uRuntime.lib and try again.
  443.  
  444. Do you get link errors on standard symbols like thePort? This is due to 
  445. bad capitalization in Symantec's libs.  Run oConv with .v checked.  This 
  446. will create a TEXT file with a .v extension.  Open that with a text 
  447. editor and correct the capitalization.  Run oConv again, with .v checked 
  448. this time too.
  449.  
  450. Do you still get errors on standard symbols? Are you using Think 
  451. C/Symantec C++ version 6 or higher? Then you must open the library (after 
  452. converting it) from Think C version *5*, and remove the unit named 
  453. %_TOOLBOX.  (If I'm not mistaken, this is the toolbox init unit, which 
  454. you won't need anyway.)
  455.  
  456. *2* Memory
  457.  
  458. 2.1) Q: What is a handle? 
  459.  
  460. A: A handle is a pointer to a pointer to something.  However, it is more 
  461. than that; creating a handle by taking the address of one of your own 
  462. pointers does NOT create a Handle; the Memory Manager will only deal 
  463. properly with Handles that are created using NewHandle or something that 
  464. calls it (such as NewRgn or GetResource).
  465.  
  466. 2.2) Q: When do I have to lock a Handle? 
  467.  
  468. A: The contents of a Handle may move, and when it does, the pointer your 
  469. handle is pointing to is changed to point to the new address so your 
  470. handle is always valid.  The toolbox may call the memory manager to 
  471. allocate more memory pretty much anytime you call it (the toolbox) and 
  472. when memory is allocated, your handle may move in memory.  Don't 
  473. dereference a handle into a pointer (or take the address of a field in a 
  474. record a handle is double-pointing to) and then call the toolbox and 
  475. expect the pointer to still be valid.  The only way to ensure that the 
  476. pointer will still be valid is to call HLock on the handle to lock it.
  477.  
  478. Use HGetState and HSetState to save & restore the "locked" state of a 
  479. handle when you lock it.
  480.  
  481. 2.3) Q: How do I dispose of Handles? 
  482.  
  483. A: DisposeHandle (formerly called DisposHandle) once and ONLY once will 
  484. do the trick.  Trying to dispose of an already disposed Handle is an 
  485. error.  DoubleTrouble (see above) will catch such bugs when they do 
  486. occur.
  487.  
  488. 2.4) Q: What about resources? 
  489.  
  490. A: Calling GetResource returns NULL if the resource is not found or 
  491. there is not enough memory, else it returns a handle to the resource.  
  492. This handle may be moved or locked like any other handle, but DO NOT 
  493. call DisposeHandle to get rid of a resource handle - call 
  494. ReleaseResource.  DisposeResource (see above) will catch this kind of 
  495. bug.
  496.  
  497. Remember that AddResource makes a resource handle out of an ordinary 
  498. handle, and RmveResource or DetachResource makes an ordinary handle out 
  499. of a resource handle.  You cannot call AddResource with a resource 
  500. handle; you have to DetachResource it first.
  501.  
  502. Resource handles are automagically disposed when the resource file they 
  503. belong to is closed.
  504.  
  505. 2.5) Q: I'm trying to use a largish array in Think C, but get a "code 
  506. overflow" error.  This is valid C, why doesn't it work?
  507.  
  508. A: The ANSI standard does not guarantee that any structure larger than 
  509. 32767 bytes be correctly handled.  Because of historical constraints, 
  510. the Mac memory model is built around several small blocks of size 32K or 
  511. less; these are used both for code and global/static data.  If you want 
  512. to use more code or data, you have to turn on "far code" or "far data" - 
  513. you still will not get around the restriction of 32K code or data per 
  514. compiled file, though.
  515.  
  516. This is one area where CodeWarrior shines; it works around most such 
  517. limitations and it doesn't cost much in performance either!
  518.  
  519. As opposed to, say, DOS or Windows, however, you can allocate as much 
  520. memory as you want (and there is in the machine) and step through it 
  521. using ordinary pointers; it's just that global and static data space is 
  522. addressed off the A5 register using a 16bit displacement addressing mode 
  523. in the 68000 processor.
  524.  
  525. On the PowerPC, everything is 32bit from the start; that runtime model 
  526. is much more like UNIX because it's taken from IBMs AIX.  No limitations 
  527. there.
  528.  
  529. *3* User interaction and You
  530.  
  531. 3.1) Q: How do I read the modifier keys of the keyboard? 
  532.  
  533. A: Just call EventAvail and check the event.modifiers field. 
  534. Only works when you are in the foreground. You can also use 
  535. GetKeys(), or (as a last resort) check the lo-mem global KeyMap 
  536. directly. 
  537.  
  538. 3.2) Q: How do I move the mouse cursor to a specific position?
  539.  
  540. A: Wait! Don't do it! There has to be a better way!
  541.  
  542. If you feel you HAVE to do it (for a game or VERY special simulation 
  543. situation) you can use the Cursor Device Manager documented in the tech 
  544. notes on ftp.apple.com.  If that manager is not installed, as it's not 
  545. on older Macs, you can use the following code:
  546.  
  547. you need to have some low-memory globals defined.  they may be defined 
  548. in SysEqu.h.
  549.  
  550. #define MTemp 0x828
  551. #define RawMouse 0x82c
  552. #define CrsrNewCouple 0x8ce
  553.  
  554. note that CrsrNewCouple is actually a combination of two globals, just 
  555. to make our life slightly easier.
  556.  
  557. the code i use to move the mouse is:
  558.  
  559. *code*
  560. void
  561. MoveMouseTo ( Point where ) {
  562.  
  563.    HideCursor ( ) ;
  564.    * ( Point * ) RawMouse = where ;
  565.    * ( Point * ) MTemp = where ;
  566.    * ( short * ) CrsrNewCouple = -1 ;
  567.    ShowCursor ( ) ;
  568. }
  569. *end*
  570.  
  571. you need to hit a couple more global variables if you want this to work 
  572. properly in a multiple-monitor system, but i forget what they are 
  573. offhand.  poke through SysEqu.h, and you should be able to figure it out 
  574. without a problem.
  575.  
  576. On the PowerPC, these lo-mem globals may not be available for native 
  577. applications; however, all Power Macintoshes implement the Cursor Device 
  578. Manager.  All Macs made after March '93 (including Centris 650 and 610) 
  579. implement the Cursor Device Manager, in fact.
  580.  
  581. There is also a file on nada.kth.se:pub/hacks/mac-faq/MoveMouse.c which 
  582. shows how to use the Cursor Device Manager, written by an excellent 
  583. Apple engineer.  Grab!
  584.  
  585. 3.3) Q: My menus don't show up in the menu bar
  586.  
  587. A: If your menus are hiearchical, you'll have to install them manually; 
  588. GetNewMBar won't do it for you.  See also 15.2.
  589.  
  590. 3.4) Q: When the user selects my menus, I get strange results back; they 
  591. seem to have different menu IDs than my menus?
  592.  
  593. A: The Menu ID as used by the menu manager is NOT the same thing as the 
  594. MENU resource ID (used in the MBAR resource and with GetMenu()) When you 
  595. create a MENU, ResEdit sets the menu ID to the MENU resource ID, but if 
  596. you re-number the resource, you will have to open the menu in ResEdit 
  597. and change the menu ID using the "Edit MENU ID" menu item.
  598.  
  599. 3.5) Q: I use GetMenu() to find a menu in the menu bar, and then change 
  600. it, but it seems I have a memory leak OR my changes don't "punch 
  601. through"
  602.  
  603. A: GetMenu() is only intended if you don't already have the menu "in 
  604. memory." The call you should use almost all the time is GetMHandle() 
  605. which gets the handle to a menu in the current menu bar by its menu ID 
  606. (not resource id)
  607.  
  608. 3.6) Q: What about pre-emptive multitasking? 
  609.  
  610. A: To the user, the Mac multitasking method, which builds upon each 
  611. application calling WaitNextEvent, GetNextEvent or EventAvail every so 
  612. often and the Process Manager/MultiFinder switching applications only at 
  613. such calls, is at least as good as preemtive multitasking, because the 
  614. present system priotitizes user interface responsiveness over everything 
  615. else.  The only shortfall about this is formatting floppies, which locks 
  616. up the Mac CPU.  This is because the Mac floppy controller is really 
  617. stupid, and would happen even if the Mac multitasked preemptively.
  618.  
  619. There IS "real" pre-emptive multitasking available for use in Mac 
  620. applications; the expensive way is buying A/UX 3.0 which can have Mac 
  621. applications written as UNIX processes; the cheap way is installing the 
  622. Thread Manager which will allow you to create pre-emptive threads.  
  623. However, the restrictions on those threads are the same as those on Time 
  624. Manager tasks: don't call any function in an unloaded segment, and don't 
  625. call QuickDraw or any toolbox call which may move memory (which are most 
  626. ToolBox calls; paradoxally, BlockMove is safe :-) as are, surprisingly, 
  627. FSRead and FSWrite)
  628.  
  629. There are several problems with making the Mac OS preemptive; including 
  630. apps that draw outside their windows or directly to screen, user dragging 
  631. and other issues.  The system is being reimplemented for 8.0 to solve 
  632. these problems.
  633.  
  634. --------------------comp.sys.mac.programmer.info---------------------
  635. comp.sys.mac.programmer.info  is  primarily  for  distributing  FAQs,
  636. tutorials, news, and  similar  documents  related to programming  the
  637. Macintosh.  To post, email csmp_info@xplain.com
  638. -----------------------about MacTech Magazine----------------------
  639. PO Box 250055, Los Angeles, CA 90025, 310-575-4343, Fax:310-575-0925
  640. For more info, anonymous ftp to ftp.netcom.com and cd to /pub/xplain
  641. or email to the following @xplain.com : custservice, editorial, 
  642. adsales, marketing, accounting, pressreleases, progchallenge, 
  643. publisher, info
  644.